home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / subscription.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  5.1 KB  |  187 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import cgi
  5. import re
  6. import util
  7. import urllib2
  8. import urlparse
  9. import xml.dom.minidom as xml
  10. reflexiveAutoDiscoveryOpener = urllib2.urlopen
  11.  
  12. def parseFile(path):
  13.     
  14.     try:
  15.         subscriptionFile = open(path, 'r')
  16.         content = subscriptionFile.read()
  17.         subscriptionFile.close()
  18.         return parseContent(content)
  19.     except:
  20.         pass
  21.  
  22.  
  23.  
  24. def parseContent(content):
  25.     
  26.     try:
  27.         dom = xml.dom.minidom.parseString(content)
  28.         root = dom.documentElement
  29.         if root.nodeName == 'rss':
  30.             urls = _getSubscriptionsFromRSSChannel(root)
  31.         elif root.nodeName == 'feed':
  32.             urls = _getSubscriptionsFromAtomFeed(root)
  33.         elif root.nodeName == 'opml':
  34.             urls = _getSubscriptionsFromOPMLOutline(root)
  35.         else:
  36.             urls = None
  37.         dom.unlink()
  38.         return urls
  39.     except:
  40.         import traceback
  41.         if util.chatter:
  42.             print 'WARNING: Error parsing OPML content...'
  43.             traceback.print_exc()
  44.         
  45.         return None
  46.  
  47.  
  48.  
  49. def get_urls_from_query(query):
  50.     urls = []
  51.     for key, value in cgi.parse_qs(query).items():
  52.         if re.match('url\\d+$', key):
  53.             urls.append(value[0])
  54.             continue
  55.     
  56.     return urls
  57.  
  58.  
  59. def findSubscribeLinks(url):
  60.     """Given a URL, test if it's trying to subscribe the user using
  61.     subscribe.getdemocracy.com.  Returns the list of parsed URLs.
  62.     """
  63.     
  64.     try:
  65.         (scheme, host, path, params, query, frag) = urlparse.urlparse(url)
  66.     except:
  67.         return ('none', [])
  68.  
  69.     if host not in ('subscribe.getdemocracy.com', 'subscribe.getmiro.com'):
  70.         return ('none', [])
  71.     
  72.     if path in ('/', '/opml.php'):
  73.         return ('feed', get_urls_from_query(query))
  74.     elif path in ('/download.php', '/download', '/download/'):
  75.         return ('download', get_urls_from_query(query))
  76.     elif path in ('/channelguide.php', '/channelguide', '/channelguide/'):
  77.         return ('guide', get_urls_from_query(query))
  78.     else:
  79.         return ('feed', [
  80.             urllib2.unquote(path[1:])])
  81.  
  82.  
  83. def _getSubscriptionsFromRSSChannel(root):
  84.     
  85.     try:
  86.         channel = root.getElementsByTagName('channel').pop()
  87.         urls = _getSubscriptionsFromAtomLinkConstruct(channel)
  88.         if urls is not None:
  89.             return urls
  90.         else:
  91.             link = channel.getElementsByTagName('link').pop()
  92.             href = link.firstChild.data
  93.             return _getSubscriptionsFromReflexiveAutoDiscovery(href, 'application/rss+xml')
  94.     except:
  95.         pass
  96.  
  97.  
  98.  
  99. def _getSubscriptionsFromAtomFeed(root):
  100.     
  101.     try:
  102.         urls = _getSubscriptionsFromAtomLinkConstruct(root)
  103.         if urls is not None:
  104.             return urls
  105.         else:
  106.             link = _getAtomLink(root)
  107.             rel = link.getAttribute('rel')
  108.             if rel == 'alternate':
  109.                 href = link.getAttribute('href')
  110.                 return _getSubscriptionsFromReflexiveAutoDiscovery(href, 'application/atom+xml')
  111.     except:
  112.         pass
  113.     
  114.  
  115.  
  116.  
  117. def _getSubscriptionsFromAtomLinkConstruct(node):
  118.     
  119.     try:
  120.         link = _getAtomLink(node)
  121.         if link.getAttribute('rel') in ('self', 'start'):
  122.             href = link.getAttribute('href')
  123.             return [
  124.                 href]
  125.     except:
  126.         pass
  127.  
  128.  
  129.  
  130. def _getSubscriptionsFromReflexiveAutoDiscovery(url, ltype):
  131.     
  132.     try:
  133.         urls = list()
  134.         html = reflexiveAutoDiscoveryOpener(url).read()
  135.         for match in re.findall('<link[^>]+>', html):
  136.             altMatch = re.search('rel="alternate"', match)
  137.             typeMatch = re.search('type="%s"' % re.escape(ltype), match)
  138.             hrefMatch = re.search('href="([^"]*)"', match)
  139.             if None not in (altMatch, typeMatch, hrefMatch):
  140.                 href = hrefMatch.group(1)
  141.                 urls.append(href)
  142.                 continue
  143.     except:
  144.         urls = None
  145.  
  146.     if len(urls) == 0:
  147.         urls = None
  148.     
  149.     return urls
  150.  
  151.  
  152. def _getAtomLink(node):
  153.     return node.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link').pop()
  154.  
  155.  
  156. def _getSubscriptionsFromOPMLOutline(root):
  157.     
  158.     try:
  159.         urls = list()
  160.         body = root.getElementsByTagName('body').pop()
  161.         _searchOPMLNodeRecursively(body, urls)
  162.     except:
  163.         urls = None
  164.  
  165.     if len(urls) == 0:
  166.         urls = None
  167.     
  168.     return urls
  169.  
  170.  
  171. def _searchOPMLNodeRecursively(node, urls):
  172.     
  173.     try:
  174.         children = node.childNodes
  175.         for child in children:
  176.             if hasattr(child, 'getAttribute'):
  177.                 if child.hasAttribute('xmlUrl'):
  178.                     url = child.getAttribute('xmlUrl')
  179.                     urls.append(url)
  180.                 else:
  181.                     _searchOPMLNodeRecursively(child, urls)
  182.             child.hasAttribute('xmlUrl')
  183.     except:
  184.         pass
  185.  
  186.  
  187.